package com.ziyueyuwen.xiniao.controller.manage;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.util.CacheUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.ziyueyuwen.xiniao.base.BaseUtil.checkParam;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;
import static com.ziyueyuwen.xiniao.util.ConstUtil.PARAM_KEY;

/**
 * @author jun 2018/12/4
 */
@Controller
@RequestMapping("/manage/cache")
public class ManageCacheController {

    @RequestMapping(value = "/reload.do", method = RequestMethod.POST)
    public void reload(@RequestParam(PARAM_KEY) String key,
                       HttpServletRequest request,
                       HttpServletResponse response) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("重新加载缓存");
        try {
            // check.
            checkParam(PARAM_KEY, key);
            // do.
            CacheUtil.setNextUpdateTimeToMem(key);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
}
